import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

/**
 * 程序的入口
 * @Author Stark
 * @Date 2022/1/23 17:57
 */
public class Main {
    //静态常量,为login准备的
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        //得到管理员或普通用户的对象,向上转型接受
        User user = login();
        //创建书架类
        BookList bookList = new BookList();
        int choice = 0;
        while((choice = user.menu()) != -1){
            user.doOperation(choice,bookList);
        }
        System.out.println("byebye");
        System.out.println("欢迎下次使用！");
    }

    //初始用户的登陆函数
    public static User login(){
        //设置初始的管理员
        AdminUser admin = new AdminUser("admin");
        while(true) {
            System.out.println("请输入用户名：(admin为管理员)");
            String name = scanner.next();
            if ("admin".equals(name)) {
                System.out.print("请输入密码：");
                for (int i = 0; i < 3; i++) {
                    String password = scanner.next();
                    if (admin.getPassword().equals(password)) {
                        return admin;
                    } else {
                        if (i < 2)
                            System.out.print("密码错误，请重新输入：");
                    }
                }
            }else{
                //不是管理员,创建普通用户对象
                return new NormalUser(name);
            }
        }

    }
}
